﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json; // 需要安装 Newtonsoft.Json 库

class Program
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        // URL 地址
        string url = "https://gitee.com/xmaoye/multiple-data-acquisition/raw/master/%E6%95%B0%E6%8D%AE.json";

        try
        {
            // 下载 JSON 数据
            string rawData = await DownloadPageContentAsync(url);

            // 替换 "/n" 为系统换行符
            string processedData = rawData.Replace("/n", Environment.NewLine);

            // 按行分割数据
            string[] lines = processedData.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            // 初始化数据存储
            List<string> dataStorage = new List<string>();

            // 模拟列表框
            List<(string Name, int UID)> listBoxItems = new List<(string Name, int UID)>();

            // 逐行解析 JSON
            foreach (string line in lines)
            {
                try
                {
                    // 解析 JSON 数据
                    var jsonData = JsonConvert.DeserializeObject<Dictionary<string, object>>(line);

                    // 获取 Name 和 UID 并添加到列表框
                    string name = jsonData["Name"]?.ToString();
                    int uid = int.Parse(jsonData["UID"]?.ToString() ?? "0");
                    listBoxItems.Add((name, uid));

                    // 获取 Data 并存储
                    string data = jsonData["Data"]?.ToString();
                    dataStorage.Add(data);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"解析错误: {ex.Message}");
                }
            }

            // 输出结果
            Console.WriteLine("列表框内容:");
            foreach (var item in listBoxItems)
            {
                Console.WriteLine($"Name: {item.Name}, UID: {item.UID}");
            }

            Console.WriteLine("\n数据存储内容:");
            foreach (var data in dataStorage)
            {
                Console.WriteLine(data);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"数据读取失败: {ex.Message}");
        }
    }

    // 从指定 URL 下载 JSON 数据
    static async Task<string> DownloadPageContentAsync(string url)
    {
        using (HttpClient client = new HttpClient())
        {
            // 发起 GET 请求
            HttpResponseMessage response = await client.GetAsync(url);

            // 确保请求成功
            response.EnsureSuccessStatusCode();

            // 获取网页内容（文本）
            return await response.Content.ReadAsStringAsync();
        }
    }




}